package com.java110.mall.goods.cmd.housekeepingServ;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.shop.ShopDto;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.mall.goods.bmo.housekeepingServ.IGetHousekeepingServBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.cache.UrlCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "housekeepingServ.queryAdminServ")
public class QueryAdminServCmd extends Cmd {

    @Autowired
    private IGetHousekeepingServBMO getHousekeepingServBMOImpl;

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        HousekeepingServDto housekeepingServDto = BeanConvertUtil.covertBean(reqJson, HousekeepingServDto.class);

        int count = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);

        List<HousekeepingServDto> housekeepingServDtos = null;
        if (count > 0) {
            housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
            freshShopName(housekeepingServDtos);
            freshCovertPhoto(housekeepingServDtos);
        } else {
            housekeepingServDtos = new ArrayList<>();
        }

//        ResponseEntity<String> responseEntity = getHousekeepingServBMOImpl.get(housekeepingServDto);
//
//        context.setResponseEntity(responseEntity);
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServDto.getRow()), count, housekeepingServDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    private void freshCovertPhoto(List<HousekeepingServDto> housekeepingServDtos) {
        if (ListUtil.isNull(housekeepingServDtos)) {
            return;
        }

        List<String> servIds = new ArrayList<>();
        for (HousekeepingServDto housekeepingServDto : housekeepingServDtos) {
            servIds.add(housekeepingServDto.getServId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(servIds.toArray(new String[servIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        FileRelDto fileRel = null;
        if (ListUtil.isNull(fileRelDtos)) {
            return;
        }
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        String coverPhoto = "";
        for (HousekeepingServDto housekeepingServDto : housekeepingServDtos) {
            for (FileRelDto tFileRelDto : fileRelDtos) {
                if (!housekeepingServDto.getServId().equals(tFileRelDto.getObjId())) {
                    continue;
                }
                coverPhoto = tFileRelDto.getFileSaveName();
                if (!StringUtil.isEmpty(coverPhoto) && !coverPhoto.startsWith("http")) {
                    coverPhoto = imgUrl + coverPhoto;
                }
                //封面图
                housekeepingServDto.setCoverPhoto(coverPhoto);
            }
        }

    }

    private void freshShopName(List<HousekeepingServDto> housekeepingServDtos) {
        if (ListUtil.isNull(housekeepingServDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();
        String mallApp = UrlCache.getMallAppUrl();
        mallApp = mallApp.endsWith("/") ? mallApp : mallApp + "/";
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (HousekeepingServDto tmpHousekeepingServDto : housekeepingServDtos) {
            tmpHousekeepingServDto.setPageUrl(mallApp + "#/pages/homemaking/detail?servId=" + tmpHousekeepingServDto.getServId() + "&shopId=" + tmpHousekeepingServDto.getShopId());
            shopIds.add(tmpHousekeepingServDto.getShopId());
            tmpHousekeepingServDto.setDefaultFee(housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(tmpHousekeepingServDto.getShopId(), tmpHousekeepingServDto.getServId()));

        }


        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        for (HousekeepingServDto tmpHousekeepingServDto : housekeepingServDtos) {
            for (ShopDto tmpShopDto : shopDtos) {
                if (!tmpShopDto.getShopId().equals(tmpHousekeepingServDto.getShopId())) {
                    continue;
                }
                tmpHousekeepingServDto.setShopName(tmpShopDto.getShopName());
            }
        }
    }
}
